package com.cskaoyan.weixin.service;


import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.Footprint;
import com.cskaoyan.bean.User1;

import com.cskaoyan.bean.good.Goods;
import com.cskaoyan.mapper.*;
import com.cskaoyan.weixin.bean.goods.*;
import com.cskaoyan.weixin.bean.search.SearchHistory;
import com.github.pagehelper.PageHelper;
import javafx.beans.binding.ListBinding;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WXGoodsServiceImpl implements WXGoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    IssueMapper issueMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    SearchWxService searchWxService;

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    KeywordMapper keywordMapper;

    /**
     * 商品数量
     *
     * @return
     */
    @Override
    public GoodsCount queryCount() {
        GoodsCount goodsCount = new GoodsCount();
        Integer count = goodsMapper.goodscount();
        goodsCount.setGoodsCount(count);
        return goodsCount;
    }

    /**
     * 商品信息
     *
     * @param id
     * @return
     */
    @Override
    public WxGoodsDetail queryGoodsDetail(Integer id) {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        if (subject != null && principals != null) {
            addFootprint(subject, id);
        }


        WxGoodsDetail wxgoodsDetail = new WxGoodsDetail();
        wxgoodsDetail.setShareImage("");

        //specification
        List<SpecificationListBean> specificationList = new ArrayList<>();
        List<String> names = goodsSpecificationMapper.selectSpecification(id);
        for (String name : names) {
            SpecificationListBean specificationListBean = new SpecificationListBean();
            specificationListBean.setName(name);
            List<ValueListBean> valueList = goodsSpecificationMapper.selectValueListBean(name, id);
            specificationListBean.setValueList(valueList);
            specificationList.add(specificationListBean);
        }
        wxgoodsDetail.setSpecificationList(specificationList);
        //goods表
        Goods info = goodsMapper.selectInfo(id);
        wxgoodsDetail.setInfo(info);
        //attribute表
        List<AttributeBean> attributeList = goodsAttributeMapper.selectByattribute(id);
        wxgoodsDetail.setAttribute(attributeList);
        //product表
        List<ProductListBean> productList = goodsProductMapper.selectProduct(id);
        wxgoodsDetail.setProductList(productList);
        //brand表
            BrandBean brand = brandMapper.selectByBrand(id);
        wxgoodsDetail.setBrand(brand);
        //issue表
        List<IssueBean> issueList = issueMapper.selectIssue();
        wxgoodsDetail.setIssue(issueList);
        //comment表
        List<DataBean> data = commentMapper.selectComment(id);
        int size = data.size();
        CommentBean commentBean = new CommentBean();
        commentBean.setCount(size);
        commentBean.setData(data);
        wxgoodsDetail.setComment(commentBean);

        //没有数据的表
        List<String> groupon = new ArrayList<>();
        wxgoodsDetail.setGroupon(groupon);

        //是否被收藏
        User1 user = (User1) subject.getPrincipal();
        if (user == null) {
            wxgoodsDetail.setUserHasCollect(0);
        }else {
            Integer userId = user.getId();
            Integer userHasCollect = commentMapper.selectUserHasCollect(userId, id);
            wxgoodsDetail.setUserHasCollect(userHasCollect);
        }
        return wxgoodsDetail;
    }

    private void addFootprint(Subject subject, Integer goodsId) {
        User1 user = (User1) subject.getPrincipals().getPrimaryPrincipal();
        Footprint footprint = new Footprint();
        footprint.setDeleted(false);
        Date date = new Date();
        footprint.setAddTime(date);
        footprint.setUpdateTime(date);
        footprint.setGoodsId(goodsId);
        footprint.setUserId(user.getId());
        footprintMapper.insertSelective(footprint);
    }

    @Override
    public WxCategory queryGoodsCategory(Integer id) {
//
//        WxCategory wxCategory=new WxCategory();
//
//        CurrentCategoryBean currentCategoryBean=categoryMapper.selectCurrent(id);
//        ParentCategoryBean  parentCategoryBean=categoryMapper.selectParent(id);
//        List<BrotherCategoryBean> brotherCategoryBeanList=categoryMapper.selectBrother(id);
//
//        wxCategory.setCurrentCategory(currentCategoryBean);
//        wxCategory.setBrotherCategory(brotherCategoryBeanList);
//        wxCategory.setParentCategory(parentCategoryBean);
//
//=======
        Category nowCategory = categoryMapper.selectByPrimaryKey(id);

        List<Category> brotherCategory = null;
        Category currentCategory = null;
        Category parentCategory = null;
        if ("L1".equals(nowCategory.getLevel())) {
            brotherCategory = categoryMapper.queryCatalogListByPid(id);
            currentCategory = brotherCategory.get(0);
            parentCategory = nowCategory;
        } else {
            brotherCategory = categoryMapper.queryCatalogListByPid(nowCategory.getPid());
            currentCategory = nowCategory;
            parentCategory = categoryMapper.selectByPrimaryKey(nowCategory.getPid());
        }

        WxCategory wxCategory = new WxCategory();
        wxCategory.setCurrentCategory(currentCategory);
        wxCategory.setBrotherCategory(brotherCategory);
        wxCategory.setParentCategory(parentCategory);

        return wxCategory;
    }

    @Override
    public WxGoodsList queryGoodsList(Integer categoryId, Integer page, Integer size, String keyword) {
        if (keyword != null) {
            PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
            if(principals != null){
                User1 user = (User1) principals.getPrimaryPrincipal();
                Integer select = keywordMapper.selectSearchHistoryByKeyword(keyword);
                if (select == null || select == 0) {
                    SearchHistory searchHistory = new SearchHistory();
                    searchHistory.setUserId(user.getId());
                    searchHistory.setKeyword(keyword);
                    searchHistory.setFrom("wx");
                    Date date = new Date();
                    searchHistory.setAddTime(date);
                    searchHistory.setUpdateTime(date);
                    searchHistory.setDeleted(false);
                    keywordMapper.insertSearchHistory(searchHistory);
                }
            }

        }

        PageHelper.startPage(page, size);
        WxGoodsList wxGoodsList = new WxGoodsList();
        Integer count = goodsMapper.goodsListcount(categoryId);
        if (keyword != null) {
            keyword = "%" + keyword + "%";
            goodsListBeans = goodsMapper.selectGoodsByKey(keyword);
        } else {
            goodsListBeans = goodsMapper.selectGoods(categoryId);
        }
        List<FilterCategoryListBean> filterCategoryListBeans = categoryMapper.selectCategory();

        wxGoodsList.setCount(count);
        wxGoodsList.setFilterCategoryList(filterCategoryListBeans);
        wxGoodsList.setGoodsList(goodsListBeans);
        return wxGoodsList;
    }

        private List<GoodsListBean> goodsListBeans;
        @Override
        public List<GoodsListBean> queryGoods (Integer categoryId){
            goodsListBeans = goodsMapper.selectGoodsListBean(categoryId);
            return goodsListBeans;
        }

    }

